import Vuex from "vuex";
import Vue from "vue";
import { getToken, setToken, removeToken } from "@/utils/token";
import { login, getUserInfo, logout, register } from "@/api/blog";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    id: "",
    account: "",
    name: "",
    avatar: "",
    admin: 0,
    token: getToken(),
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_ACCOUNT: (state, account) => {
      state.account = account;
    },
    SET_NAME: (state, name) => {
      state.name = name;
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar;
    },
    SET_ID: (state, id) => {
      state.id = id;
    },
    SET_ADMIN: (state, admin) => {
      state.admin = admin;
    },
  },
  actions: {
    // 登录
    login({ commit }, user) {
      return new Promise((resolve, reject) => {
        login(user.account, user.password)
          .then((resp) => {
            console.log("login-resp", resp.result);
            commit("SET_TOKEN", resp.result["Oauth-Token"]);
            setToken(resp.result["Oauth-Token"]);
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // 获取用户信息
    getUserInfo({ commit, state }) {
      let that = this;
      return new Promise((resolve, reject) => {
        getUserInfo()
          .then((resp) => {
            if (resp.result) {
              console.log("resp.result", resp.result);
              commit("SET_ACCOUNT", resp.result.account);
              commit("SET_NAME", resp.result.nickname);
              commit("SET_AVATAR", resp.result.avatarName);
              commit("SET_ID", resp.result.id);
              commit("SET_ADMIN", resp.result.admin);
              console.log(state.id);
            } else {
              commit("SET_ACCOUNT", "");
              commit("SET_NAME", "");
              commit("SET_AVATAR", "");
              commit("SET_ID", "");
              removeToken();
            }
            resolve(resp);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // 退出
    logout({ commit, state }) {
      console.log("logout");
      return new Promise((resolve, reject) => {
        logout()
          .then((resp) => {
            commit("SET_TOKEN", "");
            commit("SET_ACCOUNT", "");
            commit("SET_NAME", "");
            commit("SET_AVATAR", "");
            commit("SET_ID", "");
            removeToken();
            resolve();
            this.$route.push("/");
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // 前端 登出
    fedLogOut({ commit }) {
      return new Promise((resolve) => {
        commit("SET_TOKEN", "");
        commit("SET_ACCOUNT", "");
        commit("SET_NAME", "");
        commit("SET_AVATAR", "");
        commit("SET_ID", "");
        removeToken();
        resolve();
      }).catch((error) => {
        reject(error);
      });
    },
    // 注册
    register({ commit }, user) {
      return new Promise((resolve, reject) => {
        register(user.account, user.nickname, user.password)
          .then((data) => {
            commit("SET_TOKEN", data.data["Oauth-Token"]);
            setToken(data.data["Oauth-Token"]);
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
  },
});
